package com.wb3.meta.api.core.controller;

import com.wb3.meta.api.core.service.ToysChatBgService;
import com.wb3.meta.api.core.vo.ReqToysChatBgVo;
import com.wb3.meta.common.annotation.NoPermission;
import com.wb3.meta.common.annotation.RequireAuth;
import com.wb3.meta.common.authentication.RequestHolder;
import com.wb3.meta.common.plugin.BaseResultController;
import com.wb3.meta.common.pojo.ResultT;
import com.wb3.meta.common.utils.UploadFileUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;

@RestController
@Api(tags = "聊天背景相关接口")
@RequestMapping("base/user/chat/bg")
public class ToysChatBgController extends BaseResultController<ReqToysChatBgVo> {

    @Autowired
    private ToysChatBgService toysChatBgService;
    @Autowired
    private UploadFileUtils uploadFileUtils;
    /**
     * 列表接口
     *
     * @param reqToysChatBgVo
     */
    @RequireAuth
    @Override
    @ApiOperation("聊天背景列表接口")
    @GetMapping("list")
    public ResultT list(ReqToysChatBgVo reqToysChatBgVo) {
        return toysChatBgService.getModelMapPage(reqToysChatBgVo);
    }


    /**
     * 明细接口
     *
     * @param reqToysChatBgVo
     */
    @RequireAuth
    @Override
    @ApiOperation("详情接口")
    @GetMapping("detail")
    public ResultT detail(ReqToysChatBgVo reqToysChatBgVo) {
        return toysChatBgService.getModelMapById(reqToysChatBgVo.getId());
    }

    /**
     * 新增or更新接口
     *
     * @param reqToysChatBgVo
     */
    @NoPermission
    @Override
    @ApiOperation("更新接口")
    @PostMapping("update")
    public ResultT saveOrUpdate(@RequestBody ReqToysChatBgVo reqToysChatBgVo) {
        return toysChatBgService.saveOrUpdate(reqToysChatBgVo);
    }

    /**
     * 删除接口
     *
     * @param reqToysChatBgVo
     */
    @NoPermission
    @Override
    @ApiOperation("删除接口")
    @PostMapping("delete")
    public ResultT delete(@RequestBody ReqToysChatBgVo reqToysChatBgVo) {
        return toysChatBgService.delete(reqToysChatBgVo);
    }

    @RequireAuth
    @ApiOperation("背景图片上传")
    @PostMapping("upload")
    public ResultT upload(@RequestBody MultipartFile file) {
        // 获取文件后缀名
        String originalFilename = file.getOriginalFilename();
        String suffix = originalFilename.substring(originalFilename.lastIndexOf("."));

        // 定义允许的图片格式
        Set<String> allowedFormats = new HashSet<>(Arrays.asList(".jpg", ".jpeg", ".png", ".gif"));

        // 检查文件格式
        if (!allowedFormats.contains(suffix.toLowerCase())) {
            // 如果文件格式不正确，返回错误信息
            return ResultT.fail("只能上传图片");
        }

        // 生成新的文件名
        String filename = System.currentTimeMillis() + "-" + RequestHolder.getUserId() + suffix;

        // 上传文件
        return uploadFileUtils.uploadFileToAliyun("user/chat/bg", filename, file);
    }

}
